package Function;

import java.util.ArrayList;
import java.util.Stack;

public class inFixTopostFix {
	private String s;
	private String post;
	private String priorty = "^/*+-";

	public inFixTopostFix(String s) {
		this.s = s;
		converte();
	}

	private void converte() {
		/**
		 * Assumbtions sin = s cos = c
		 * 
		 * available operations is + - * / ^
		 * 
		 * priorty (descending)
		 * 
		 * ^ / * + -
		 * 
		 */
		ArrayList<Unit> post = new ArrayList<Unit>();
		Stack<Character> oper = new Stack<Character>();
		// to make it easier replace each sin and cos with s and c
		// corrispondingly
		s = s.replace("sin", "s");
		s = s.replace("cos", "c");
		char c;
		for (int i = 0; i < s.length(); i++) {
			c = s.charAt(i);
			if (isOperator(c)) {
				while (!oper.isEmpty() && compare(oper.peek(), c))
					post.add(new Unit(oper.pop()));
				oper.push(c);
			} else if (isFunction(c)) {
				if (c == 's') {

				} else {

				}
			} else { // number
				if (c == 'x') {

				} else {

				}

			}
		}
	}

	/**
	 * 
	 * @param op1
	 *            first operator
	 * @param op2
	 *            second operator
	 * @return true if op1 > op2 i.e op1 has higher precedence than op2
	 */
	public boolean compare(char op1, char op2) {
		return priorty.indexOf(op1) < priorty.indexOf(op2); // yes it's < .
	}

	public boolean isOperator(char c) {
		return priorty.contains(c + "");
	}

	public boolean isFunction(char c) {
		return c == 's' || c == 'c' || c == 'e';
	}

	public boolean isNumber(char c) {
		return (c >= '0' && c <= '9') || c == '.';
	}

	public String getPostFix() {
		return post;
	}
}
